<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mstatics extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    public function getIDCampaign($id)
    {
        return $this->db->query('SELECT id FROM campaign WHERE id_shop = '.$id.' ORDER BY id DESC LIMIT 1')->row_object()->id;
    }
    public function getCampaignDB($number,$offset,$id_user)
    {
        $limit = $offset ? intval($offset) : '0';
        $id = array();
        
        $sql = "SELECT *,DATE_FORMAT(created, '%H:%i:%s %d-%m-%Y') AS time FROM campaign WHERE 1=1";
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (title like '%".$key."%' OR ";
            $sql .= " code_campaign like '%".$key."%') ";
        }
        $sql .= " ORDER BY id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        
        foreach($xs AS $el=>$le)
        {
            $id[] = $le->id;
            $xs[$el]->count_view = $this->m_db->where(array('id_campaign' => intval($le->id)))->count('log_access');
        }
        //$id_ = substr($id__, 1);
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count, 'idCampaign' => $id);
        
    }
    
    public function getCountTime($id_user,$time,$id_campaign,$number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        $array = array();
        $dem = 0;
        $key = '';
        $array_device = array();
        $dem_device = 0;
        $key_device = '';
        
        $show_time = array();
        $dem_os = 0;
        $key_os = '';
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            $data = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->limit($number)->offset($offset)->order_by(array('time_vs' => 'DESC'))->get('log_access');
            
       
            foreach($data AS $le=>$el)
            {
                //$dem =  $dem + 1;
                if($el['show_full_time'] == 'Other')
                {
                    $key = 'Chưa xác định';
                }else if(in_array($el['show_full_time'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    $key = '';
                    $key = $el['show_full_time'];
                }
                @$show_time[$key] = $show_time[$key] + 1;
            }
            //$array['tong_browser'] = $dem;
            //arsort($show_time);
            
            
        }
        return $show_time;
    }
    
    public function getCount($id_user,$time,$id_campaign)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        $array = array();
        $dem = 0;
        $key = '';
        $array_device = array();
        $dem_device = 0;
        $key_device = '';
        
        $array_os = array();
        $dem_os = 0;
        $key_os = '';
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            $data = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->get('log_access');
            
       
            foreach($data AS $le=>$el)
            {
                
                if($el['browser_name_group'] == 'Other')
                {
                    $dem =  $dem + 1;
                    $key = 'Chưa xác định';
                }else if(in_array($el['browser_name_group'], array(null, '', '0')))
                {
                    $dem =  $dem + 1;
                    $key = 'Chưa xác định';
                }else{
                    $dem =  $dem + 1;
                    $key = '';
                    $key = $el['browser_name_group'];
                }
                @$array[$key] = $array[$key] + 1;
                
                
                if($el['device_name'] == 'Other')
                {
                    $dem_device =  $dem_device + 1;
                    $key_device = 'Máy tính';
                }else{
                    $dem_device =  $dem_device + 1;
                    $key_device = 'Di động';
                }
                @$array_device[$key_device] = $array_device[$key_device] + 1;
                
                if(in_array($el['os_name'], array(null, '', '0')))
                {
                    $dem_os =  $dem_os + 1;
                    $key_os = 'Chưa xác định';
                }else{
                    $dem_os =  $dem_os + 1;
                    $key_os = '';
                    $key_os = $el['os_name'];
                }
                @$array_os[$key_os] = $array_os[$key_os] + 1;
                
                
                if($el['browser_name'] == 'Other')
                {
                    $key = 'Chưa xác định';
                }else if(in_array($el['browser_name'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    $key = '';
                    $key = $el['browser_name'];
                }
                @$browser_name[$key] = $browser_name[$key] + 1;
                
                if($el['device_name'] == 'Other')
                {
                    $key = 'Máy tính ( Chưa xác định )';
                }else if(in_array($el['device_name'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    $key = '';
                    $key = $el['device_name'];
                }
                @$device_name[$key] = $device_name[$key] + 1;
            }
            $array['tong_browser'] = $dem;
            asort($array);
            $array_device['tong_device'] = $dem_device;
            asort($array_device);
            $array_os['tong_os'] = $dem_os;
            asort($array_os);
            //$array['tong_browser'] = $dem;
            arsort($browser_name);
            //$array['tong_browser'] = $dem;
            arsort($device_name);
            
            
            foreach($data AS $le=>$el)
            {
                //$dem =  $dem + 1;
                if($el['show_full_time'] == 'Other')
                {
                    $key = 'Chưa xác định';
                }else if(in_array($el['show_full_time'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    $key = '';
                    $key = $el['show_full_time'];
                }
                @$show_time[$key] = $show_time[$key] + 1;
            }
            //$array['tong_browser'] = $dem;
            //arsort($show_time);
            foreach($data AS $le=>$el)
            {
                //$dem =  $dem + 1;
                if($el['os_group'] == 'Other')
                {
                    $key = 'Chưa xác định';
                }else if(in_array($el['os_group'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    $key = '';
                    $key = $el['os_group'];
                }
                @$os_group[$key] = $os_group[$key] + 1;
            }
            //$array['tong_browser'] = $dem;
            asort($os_group);
            
        }
        return (object)array('os_group' => $os_group, 'show_time' => $show_time, 'device_name' => $device_name,'browser' => $array,'browser_ver' => $browser_name,'device' => $array_device, 'os' => $array_os);
    }
    
    public function getLogAccess($number,$offset,$id_user,$id_campaign,$time)
    {
        $limit = $offset ? intval($offset) : '0';
        $id = array();
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            $count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            $data = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->limit($number)->offset($offset)->order_by(array('time_vs' => 'DESC'))->get('log_access');
        }
        return array('count' => $count, 'data' => $data);
    }
    
    
    public function getChartAb($id = 2, $time, $type, $id_user)
    {
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id);
        
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            
            $data = $this->m_db->where(array('id_campaign' => $id))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->get('log_access');
            
            $dem = 0;
            foreach($data AS $le=>$el)
            {
                $dem =  $dem + 1;
                if($el['show_time'] == 'Other')
                {
                    $key = 'Other';
                }else if(in_array($el['show_time'], array(null, '', '0')))
                {
                    $key = 'Other';
                }else{
                    $key = '';
                    $key = $el['show_time'];
                }
                @$array[$key] = $array[$key] + 1;
            }
            $array['tong'] = $dem;
            //ksort($array);
            return $array;
        }
        return 201;
        
    }
    
    
    public function getDevice($id_user,$time,$id_campaign,$type)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();

        $array_device = array();
        $dem_device = 0;
        $key_device = '';
        
        $array_device_colum = array();
        $dem_device_colum = 0;
        $key_device_colum = '';
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            $data = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->get('log_access');
            
            foreach($data AS $le=>$el)
            {
                $dem_device =  $dem_device + 1;
                if($el['device_name'] == 'Other')
                {
                    $key_device = 'Máy tính';
                }else{
                    $key_device = $el['device_name'];
                }
                @$array_device[$key_device] = $array_device[$key_device] + 1;
            }
            $array_device['tong_device'] = $dem_device;
            arsort($array_device);
            
            foreach($data AS $le=>$el)
            {
                $dem_device_colum =  $dem_device_colum + 1;
                if($el['device_name'] == 'Other')
                {
                    $key_device_colum = 'Máy tính';
                }else{
                    $key_device_colum = 'Di động';
                }
                @$array_device_colum[$key_device_colum] = $array_device_colum[$key_device_colum] + 1;
            }
            $array_device_colum['tong_device_colum'] = $dem_device_colum;
            asort($array_device_colum);
            
        }
        return (object)array('table' => $array_device, 'colum' => $array_device_colum);
    }
    
    public function getBrowser($id_user,$time,$id_campaign,$type)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();

        $array_device = array();
        $dem_device = 0;
        $key_device = '';
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            $data = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->get('log_access');
            
            foreach($data AS $le=>$el)
            {
                $dem_device =  $dem_device + 1;
                if($el['browser_name_group'] == 'Other')
                {
                    $key_device = 'Other';
                }else{
                    $key_device = $el['browser_name_group'];
                }
                @$array_device[$key_device] = $array_device[$key_device] + 1;
            }
            $array_device['tong_browser'] = $dem_device;
            $array_table = $array_device;
            $array_device_chart = $array_device;
            
            asort($array_device_chart);
            arsort($array_table);
            
        }
        return (object)array('table' => $array_table, 'chart' => $array_device_chart);
    }
    
    
    public function getOsStatic($id_user,$time,$id_campaign,$type)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();

        $array_device = array();
        $dem_device = 0;
        $key_device = '';
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            $data = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->get('log_access');
            
            foreach($data AS $le=>$el)
            {
                $dem_device =  $dem_device + 1;
                if($el['os_name'] == 'Other')
                {
                    $key_device = 'Other';
                }else{
                    $key_device = $el['os_name'];
                }
                @$array_device[$key_device] = $array_device[$key_device] + 1;
            }
            $array_device['tong_os'] = $dem_device;
            
            $array_table = $array_device;
            $array_device_chart = $array_device;
            
            asort($array_device_chart);
            arsort($array_table);
            
        }
        return (object)array('table' => $array_table, 'chart' => $array_device_chart);
    }
    
    public function getPlaceStatic($id_user,$time,$id_campaign,$type)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();

        $array_device = array();
        $dem_device = 0;
        $key_device = '';
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            $data = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->get('log_access');
            
            foreach($data AS $le=>$el)
            {
                $dem_device =  $dem_device + 1;
                if($el['ip_access'] == 'Other')
                {
                    $key_device = 'Other';
                }else{
                    $key_device = $this->db->query('SELECT title FROM criteria WHERE destination ="'.$el['ip_access'].'"')->row_object()->title;
                }
                @$array_device[$key_device] = $array_device[$key_device] + 1;
            }
            $array_device['tong_place'] = $dem_device;
            
            $array_table = $array_device;
            $array_device_chart = $array_device;
            
            asort($array_device_chart);
            arsort($array_table);
            
        }
        return (object)array('table' => $array_table, 'chart' => $array_device_chart);
    }
    
    public function getTimeCampaign($id_campaign,$id_user)
    {
        $mq = $this->db->query('SELECT id,title,code_campaign, DATE_FORMAT(time_start,"%d/%m/%Y %H:%i") AS time1,DATE_FORMAT(time_end,"%d/%m/%Y %H:%i") AS time2, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time_1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time_2 FROM campaign WHERE id ='.$id_campaign)->row_object();
        
        $time_select = $mq->time1.' - '.$mq->time2;
        $time        = $mq->time_1.'|'.$mq->time_2;
        return (object)array('time_select' => $time_select, 'time' => $time, 'title' => '['.$mq->code_campaign.'] - ' . $mq->title);
    }
    public function getNameShop($id)
    {
       return $this->db->query('SELECT shopName FROM shop WHERE id = '.$id)->row_object()->shopName;
    }
    
    
    
    
    
    public function getmail($id_shop, $id_campaign, $time)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        //die($check.'--');
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            return $this->m_db->where(array('id_shop' => $id_shop))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_mail');
        }
    }
    
    public function getfacebook($id_shop, $id_campaign, $time)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            return $this->m_db->where(array('id_shop' => $id_shop))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_facebook');
        }
    }
    
	public function getmobile($id_shop, $id_campaign, $time)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            return $this->m_db->where(array('id_shop' => $id_shop))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_mobile');
        }
    }
    
    /**
     * 
     * 
     * bao cao
     * 
     */
     
     
    public function getExport($id_user,$time,$id_campaign)
    {
       
        $id = array();
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            $data = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->order_by(array('time_vs' => 'DESC'))->get('log_access');
        }
        return $data;
    }
	
	public function getExportMobile($id_user,$time,$id_campaign, $id_shop)
    {
       
        $id = array();
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            $data = $this->m_db->where(array('id_shop' => $id_shop))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->order_by(array('time_vs' => 'DESC'))->get('log_mobile');
        }
        return $data;
    }
    
    
    public function getExportEmail($id_user,$time,$id_campaign, $id_shop)
    {
       
        $id = array();
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            $data = $this->m_db->where(array('id_shop' => $id_shop))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->order_by(array('time_vs' => 'DESC'))->get('log_mail');
        }
        return $data;
    }
    
    public function getExportFacebook($id_user,$time,$id_campaign, $id_shop)
    {
       
        $id = array();
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            $data = $this->m_db->where(array('id_shop' => $id_shop))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->order_by(array('time_vs' => 'DESC'))->get('log_facebook');
        }
        return $data;
    }
    
	public function getCampaignUser($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $sql = "SELECT c.*, s.shopName FROM campaign AS c JOIN shop AS s ON c.id_shop = s.id WHERE c.id_customers = ".$this->session->userdata('id_user');
        $sql .= " ORDER BY c.id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
    }
	
	public function getListShop($id)
    {
	//die('1');
        $data = $this->db->query('SELECT id, shopName, address FROM shop ORDER BY id ASC')->result_object();
        $html = '<option value = "0">Chọn cửa hàng</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->shopName.'-'.$le->address.'</option>';
        }
        return $html;
    }
	
	public function addCampaignUser(){
	
	}
	
}